<template>
  <div class='container'>
    <el-dialog
  :title="`${data.id?'修改':'新增'}学科`"
  :visible.sync="dialogVisible"
  width="32%"
  @click="dialogVisible=false"
  >
    <el-form :model="formTitle" ref="dataForm" :rules="rules" label-width="80px">
    <el-form-item label="活动名称" prop="subjectName">
    <el-input v-model="formTitle.subjectName" placeholder="请输入学科名称"></el-input>
  </el-form-item>
      <el-form-item label="是否显示" prop="isFrontDisplay">
      <el-switch
      v-model="formTitle.isFrontDisplay"
      active-color="#13ce66"
      inactive-color="#ff4949"
      :active-value="1"
      :inactive-value="0"
    >
      </el-switch>
      </el-form-item>
    </el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible=false">取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
//
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  props: {
    data: {
      default: () => {},
      type: Object
    }
  },
  data () {
    return {
      dialogVisible: false,
      formTitle: {
        id: '',
        subjectName: '',
        isFrontDisplay: 1
      },
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }],
        isFrontDisplay: [{ type: 'number', message: '请选择是否显示', tirgger: 'blur' }]
      }

    }
  },
  methods: {
    open () {
      this.dialogVisible = true
      if (this.data.id) {
        const { id, subjectName, isFrontDisplay } = this.data
        this.formTitle = { id, subjectName, isFrontDisplay }
      } else {
        this.formTitle = {
          id: '',
          subjectName: '',
          isFrontDisplay: 1
        }
      }
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    async confirm () {
      this.$refs.dataForm.validate(async valid => {
        if (valid) {
          // 提交
          if (!this.data.id) {
            // 新增
            await add(this.formTitle)
          } else {
            // 修改
            this.formTitle.id = this.data.id
            await update(this.formTitle)
          }
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.$emit('updateList')
        }
      })
    }

  }

}
</script>

<style scoped lang='less'>

</style>
